import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class MainFrame extends JFrame {
    private JTextField inputFileField;
    private JTextField outputDirField;
    private JTextField gridSizeField;
    private JTextField roadSizeField;
    private JTextField longitudeField;
    private JTextField latitudeField;
    
    // 定义统一的字体和颜色
    private static final Font LABEL_FONT = new Font("微软雅黑", Font.PLAIN, 12);
    private static final Font TITLE_FONT = new Font("微软雅黑", Font.BOLD, 12);
    private static final Color BACKGROUND_COLOR = new Color(240, 240, 240);
    private static final Color BUTTON_COLOR = new Color(70, 130, 180);
    
    public MainFrame() {
        setTitle("空间数据处理程序");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700, 450);
        setLocationRelativeTo(null);
        setBackground(BACKGROUND_COLOR);
        
        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(BACKGROUND_COLOR);
        
        // 创建文件选择面板
        JPanel filePanel = new JPanel(new GridLayout(2, 1, 10, 10));
        filePanel.setBackground(BACKGROUND_COLOR);
        filePanel.setBorder(createTitleBorder("文件选择"));
        
        // 输入文件选择
        JPanel inputPanel = new JPanel(new BorderLayout(5, 0));
        inputPanel.setBackground(BACKGROUND_COLOR);
        inputFileField = createStyledTextField();
        JButton inputBrowseButton = createStyledButton("选择文件");
        JLabel inputLabel = createStyledLabel("输入文件：");
        inputPanel.add(inputLabel, BorderLayout.WEST);
        inputPanel.add(inputFileField, BorderLayout.CENTER);
        inputPanel.add(inputBrowseButton, BorderLayout.EAST);
        
        // 输出目录选择
        JPanel outputPanel = new JPanel(new BorderLayout(5, 0));
        outputPanel.setBackground(BACKGROUND_COLOR);
        outputDirField = createStyledTextField();
        JButton outputBrowseButton = createStyledButton("选择目录");
        JLabel outputLabel = createStyledLabel("输出目录：");
        outputPanel.add(outputLabel, BorderLayout.WEST);
        outputPanel.add(outputDirField, BorderLayout.CENTER);
        outputPanel.add(outputBrowseButton, BorderLayout.EAST);
        
        filePanel.add(inputPanel);
        filePanel.add(outputPanel);
        
        // 参数配置面板
        JPanel paramsPanel = new JPanel(new GridLayout(2, 2, 15, 15));
        paramsPanel.setBackground(BACKGROUND_COLOR);
        paramsPanel.setBorder(createTitleBorder("参数配置"));
        
        // 栅格大小
        JPanel gridPanel = createParamPanel("栅格大小：", gridSizeField = createStyledTextField());
        
        // 路网大小
        JPanel roadPanel = createParamPanel("路网大小：", roadSizeField = createStyledTextField());
        
        // 经度
        JPanel longPanel = createParamPanel("经    度：", longitudeField = createStyledTextField());
        
        // 纬度
        JPanel latPanel = createParamPanel("纬    度：", latitudeField = createStyledTextField());
        
        paramsPanel.add(gridPanel);
        paramsPanel.add(roadPanel);
        paramsPanel.add(longPanel);
        paramsPanel.add(latPanel);
        
        // 中央面板
        JPanel centerPanel = new JPanel(new BorderLayout(10, 10));
        centerPanel.setBackground(BACKGROUND_COLOR);
        centerPanel.add(filePanel, BorderLayout.NORTH);
        centerPanel.add(paramsPanel, BorderLayout.CENTER);
        
        // 执行按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 10));
        buttonPanel.setBackground(BACKGROUND_COLOR);
        JButton executeButton = createStyledButton("执行处理");
        executeButton.setPreferredSize(new Dimension(120, 35));
        buttonPanel.add(executeButton);
        
        // 添加到主面板
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        // 添加事件监听
        inputBrowseButton.addActionListener(e -> selectInputFile());
        outputBrowseButton.addActionListener(e -> selectOutputDirectory());
        executeButton.addActionListener(e -> executeProcess());
        
        add(mainPanel);
    }
    
    private JLabel createStyledLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(LABEL_FONT);
        label.setPreferredSize(new Dimension(80, 25));
        return label;
    }
    
    private JTextField createStyledTextField() {
        JTextField field = new JTextField();
        field.setFont(LABEL_FONT);
        return field;
    }
    
    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(LABEL_FONT);
        button.setForeground(Color.WHITE);
        button.setBackground(BUTTON_COLOR);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setOpaque(true);
        return button;
    }
    
    private TitledBorder createTitleBorder(String title) {
        TitledBorder border = BorderFactory.createTitledBorder(
            BorderFactory.createLineBorder(Color.GRAY),
            title,
            TitledBorder.LEFT,
            TitledBorder.TOP,
            TITLE_FONT,
            Color.DARK_GRAY
        );
        border.setTitlePosition(TitledBorder.ABOVE_TOP);
        return border;
    }
    
    private JPanel createParamPanel(String labelText, JTextField field) {
        JPanel panel = new JPanel(new BorderLayout(5, 0));
        panel.setBackground(BACKGROUND_COLOR);
        panel.add(createStyledLabel(labelText), BorderLayout.WEST);
        panel.add(field, BorderLayout.CENTER);
        return panel;
    }
    
    private void selectInputFile() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            inputFileField.setText(selectedFile.getAbsolutePath());
        }
    }
    
    private void selectOutputDirectory() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            outputDirField.setText(selectedFile.getAbsolutePath());
        }
    }
    
    private void executeProcess() {
        String inputFile = inputFileField.getText();
        String outputDir = outputDirField.getText();
        String gridSize = gridSizeField.getText();
        String roadSize = roadSizeField.getText();
        String longitude = longitudeField.getText();
        String latitude = latitudeField.getText();
        
        if (inputFile.isEmpty() || outputDir.isEmpty()) {
            showErrorMessage("请选择输入文件和输出目录！");
            return;
        }
        
        if (gridSize.isEmpty() || roadSize.isEmpty() || 
            longitude.isEmpty() || latitude.isEmpty()) {
            showErrorMessage("请填写所有参数！");
            return;
        }
        
        // TODO: 在这里添加您的处理逻辑
        showSuccessMessage(
            "处理完成！\n" +
            "输入文件：" + inputFile + "\n" +
            "输出目录：" + outputDir + "\n" +
            "栅格大小：" + gridSize + "\n" +
            "路网大小：" + roadSize + "\n" +
            "经度：" + longitude + "\n" +
            "纬度：" + latitude
        );
    }
    
    private void showErrorMessage(String message) {
        JOptionPane.showMessageDialog(this, 
            message, 
            "错误", 
            JOptionPane.ERROR_MESSAGE);
    }
    
    private void showSuccessMessage(String message) {
        JOptionPane.showMessageDialog(this, 
            message,
            "处理成功",
            JOptionPane.INFORMATION_MESSAGE);
    }
} 